package com.demo.afternoon;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @ClassName 反射5
 * @Description TODO
 * @Author tianqinglong
 * @Date 19:18 2022/7/12
 * @Version 1.0
 **/
public class 反射5 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class<?> c1 = Class.forName("com.demo.afternoon.User");
//        User user = new User();
//        Class<? extends User> aClass = user.getClass();

        System.out.println(c1.getName());
        System.out.println(c1.getSimpleName());
        System.out.println("----------");

        Field[] fields = c1.getFields();// 只能找到public属性
        Field[] declaredFields = c1.getDeclaredFields();// 找到全部的属性
        for (Field field : fields) {
            System.out.println(field+"++++++++++");
            //public int com.demo.afternoon.User.age++++++++++
        }
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField+"-----------");
            /*private int com.demo.afternoon.User.id-----------
            * private java.lang.String com.demo.afternoon.User.name-----------
private java.lang.String com.demo.afternoon.User.pwd-----------
public int com.demo.afternoon.User.age-----------*/
        }

        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("--------------");
        Field names = c1.getDeclaredField("name");
        System.out.println(names);

        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("--------------");
        Method[] methods = c1.getMethods();//获得本类及其父类的全部public方法
        for (Method method : methods) {
            System.out.println(method);
        }
        Method[] declaredMethods = c1.getDeclaredMethods(); //获得本类的所有方法
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod+"++------------");
        }
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("++++++++++++++++++++++++++");

        Method getNames = c1.getMethod("getName", null);
        Method setName = c1.getMethod("setName", String.class);
        System.out.println(getNames);
        System.out.println(setName);

        Constructor<?>[] constructors = c1.getConstructors();//public
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }
        Constructor declaredConstructor=c1.getDeclaredConstructor(int.class,String.class,String.class);
        System.out.println(declaredConstructor+"'''''''''''''''");
    }
}
